package com.dataextend.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataextend.common.utils.CommonUtil;
import com.dataextend.entity.*;
import com.dataextend.mapper.cluster.AnalysisModelMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *  事件分析模型保存查询条件
 */
@Service
public class AnalysisModelSaveService {
    @Autowired
    private AnalysisModelMapper analysisModelMapper;


    /**
     *  事件分析- 新增
     */
    public JSONObject eventAnalysisModel(EventAnalysisModel eventAnalysisModel){
        String modelName = eventAnalysisModel.getModelName();
        String startDate = eventAnalysisModel.getStartDate();
        String endDate = eventAnalysisModel.getEndDate();
        List<JSONObject> eventCodes = eventAnalysisModel.getEventCodes();
        List<String> target = eventAnalysisModel.getTarget();
        List<JSONObject> filterParameter = eventAnalysisModel.getFilterParameter();

        String modelIdNow = analysisModelMapper.searchEventModelId();
        int modelId = 0;
        if (StringUtils.isNotEmpty(modelIdNow)){
            modelId = Integer.parseInt(modelIdNow) + 2;
        } else {
            modelId = 1;
        }

        analysisModelMapper.eventAnalysisModel(String.valueOf(modelId), modelName, startDate, endDate, JSONObject.toJSONString(eventCodes),
                JSONObject.toJSONString(target), JSONObject.toJSONString(filterParameter));
        return CommonUtil.successJson();
    }


    /**
     *  事件分析- 列表
     */
    public JSONObject listEventModel(){
        List<JSONObject> list = analysisModelMapper.listEventModel();
        List<JSONObject> returnDetail = new ArrayList<>();
        for (JSONObject obj : list){
            JSONObject detail = new JSONObject(true);
            detail.put("modelId", obj.getString("model_id"));
            detail.put("modelName", obj.getString("model_name"));
            detail.put("startDate", obj.getString("start_date"));
            detail.put("endDate", obj.getString("end_date"));
            detail.put("eventCodes", JSONArray.parseArray(obj.getString("event_codes")).toJavaList(JSONObject.class));
            detail.put("target", Arrays.asList(obj.getString("target").replace("\"","").replace("[","").replace("]","").split(",")));
            detail.put("filterParameter", JSONArray.parseArray(obj.getString("filter_parameter")).toJavaList(JSONObject.class));
            returnDetail.add(detail);
        }

        JSONObject returnResult = new JSONObject(true);
        returnResult.put("list", returnDetail);
        return returnResult;
    }

    /**
     *  事件分析- 删除
     */
    public JSONObject deleteEventModel(String modelId){
        analysisModelMapper.deleteEventModel(modelId);
        return CommonUtil.successJson();
    }



    /**
     *  转化漏斗分析- 新增
     */
    public JSONObject hopperAnalysisModel(HopperAnalysisModel hopperAnalysisModel){
        String modelName = hopperAnalysisModel.getModelName();
        List<String> eventCodes = hopperAnalysisModel.getEventCodes();
        String target = hopperAnalysisModel.getTarget();
        List<JSONObject> filterParameter = hopperAnalysisModel.getFilterParameter();
        String startDate = hopperAnalysisModel.getStartDate();
        String endDate = hopperAnalysisModel.getEndDate();
        String modelIdNow = analysisModelMapper.searchHopperModelId();
        int modelId = 0;
        if (StringUtils.isNotEmpty(modelIdNow)){
            modelId = Integer.parseInt(modelIdNow) + 2;
        } else {
            modelId = 1;
        }

        analysisModelMapper.hopperAnalysisModel(String.valueOf(modelId), modelName, JSONObject.toJSONString(eventCodes), target, JSONObject.toJSONString(filterParameter), startDate, endDate);
        return CommonUtil.successJson();
    }


    /**
     *  转化漏斗分析- 列表
     */
    public JSONObject listHopperModel(){
        List<JSONObject> list = analysisModelMapper.listHopperModel();
        List<JSONObject> returnDetail = new ArrayList<>();
        for (JSONObject obj : list){
            JSONObject detail = new JSONObject(true);
            detail.put("modelId", obj.getString("model_id"));
            detail.put("modelName", obj.getString("model_name"));
            detail.put("eventCodes", Arrays.asList(obj.getString("event_codes").replace("\"","").replace("[","").replace("]","").split(",")));
            detail.put("target", obj.getString("target"));
            detail.put("filterParameter", JSONArray.parseArray(obj.getString("filter_parameter")).toJavaList(JSONObject.class));
            detail.put("startDate", obj.getString("start_date"));
            detail.put("endDate", obj.getString("end_date"));
            returnDetail.add(detail);
        }
        JSONObject returnResult = new JSONObject(true);
        returnResult.put("list", returnDetail);
        return returnResult;
    }

    /**
     *  转化漏斗分析- 删除
     */
    public JSONObject deleteHopperModel(String modelId){
        analysisModelMapper.deleteHopperModel(modelId);
        return CommonUtil.successJson();
    }



    /**
     *  留存分析- 新增
     */
    public JSONObject remainAnalysisModel(RemainAnalysisModel remainAnalysisModel){
        String modelName = remainAnalysisModel.getModelName();
        String startDate = remainAnalysisModel.getStartDate();
        String endDate = remainAnalysisModel.getEndDate();
        List<JSONObject> eventCode = remainAnalysisModel.getEventCode();
        List<JSONObject> filterParameter = remainAnalysisModel.getFilterParameter();
        String modelIdNow = analysisModelMapper.searchRemainModelId();
        int modelId = 0;
        if (StringUtils.isNotEmpty(modelIdNow)){
            modelId = Integer.parseInt(modelIdNow) + 2;
        } else {
            modelId = 1;
        }

        analysisModelMapper.remainAnalysisModel(String.valueOf(modelId), modelName, startDate, endDate,  JSONObject.toJSONString(eventCode),  JSONObject.toJSONString(filterParameter));
        return CommonUtil.successJson();
    }

    /**
     *  留存分析- 列表
     */
    public JSONObject listRemainModel(){
        List<JSONObject> list = analysisModelMapper.listRemainModel();
        List<JSONObject> returnDetail = new ArrayList<>();
        for (JSONObject obj :list){
            JSONObject detail = new JSONObject(true);
            detail.put("modelId", obj.getString("model_id"));
            detail.put("modelName", obj.getString("model_name"));
            detail.put("startDate", obj.getString("start_date"));
            detail.put("endDate", obj.getString("end_date"));
            detail.put("eventCode", JSONArray.parseArray(obj.getString("event_code")).toJavaList(JSONObject.class));
            detail.put("filterParameter", JSONArray.parseArray(obj.getString("filter_parameter")).toJavaList(JSONObject.class));
            returnDetail.add(detail);
        }
        JSONObject returnResult = new JSONObject(true);
        returnResult.put("list", returnDetail);
        return returnResult;
    }

    /**
     *  留存分析- 删除
     */
    public JSONObject deleteRemainModel(String modelId){
        analysisModelMapper.deleteRemainModel(modelId);
        return CommonUtil.successJson();
    }




    /**
     *  分布分析- 新增
     */
    public JSONObject distributeAnalysisModel(DistributeAnalysisModel distributeAnalysisModel){
        String modelName = distributeAnalysisModel.getModelName();
        String startDate = distributeAnalysisModel.getStartDate();
        String endDate = distributeAnalysisModel.getEndDate();
        String eventCodes = distributeAnalysisModel.getEventCodes();
        String dimension = distributeAnalysisModel.getDimension();
        List<JSONObject> filterParam = distributeAnalysisModel.getFilterParam();
        String modelIdNow = analysisModelMapper.searchDistributeModelId();
        int modelId = 0;
        if (StringUtils.isNotEmpty(modelIdNow)){
            modelId = Integer.parseInt(modelIdNow) + 2;
        } else {
            modelId = 1;
        }

        analysisModelMapper.distributeAnalysisModel(String.valueOf(modelId), modelName, startDate, endDate, eventCodes, dimension, JSONObject.toJSONString(filterParam));
        return CommonUtil.successJson();
    }


    /**
     *  分布分析- 列表
     */
    public JSONObject listDistributeModel(){
        List<JSONObject> list = analysisModelMapper.listDistributeModel();
        List<JSONObject> returnDetail = new ArrayList<>();
        for (JSONObject obj : list){
            JSONObject detail = new JSONObject(true);
            detail.put("modelId", obj.getString("model_id"));
            detail.put("modelName", obj.getString("model_name"));
            detail.put("startDate", obj.getString("start_date"));
            detail.put("endDate", obj.getString("end_date"));
            detail.put("eventCodes", obj.getString("event_code"));
            detail.put("dimension", obj.getString("dimension"));
            detail.put("filterParam", JSONArray.parseArray(obj.getString("filter_parameter")).toJavaList(JSONObject.class));
            returnDetail.add(detail);
        }

        JSONObject returnResult = new JSONObject(true);
        returnResult.put("list", returnDetail);
        return returnResult;
    }

    /**
     *  分布分析- 删除
     */
    public JSONObject deleteDistributeModel(String modelId){
        analysisModelMapper.deleteDistributeModel(modelId);
        return CommonUtil.successJson();
    }

}
